﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using TheSimGer.Common;
using TheSimGer.HelperFrm;
using TheSimGer.SpecialEvent;
using TheSimGer.SubFrm;
using WebSocketSharp;

namespace TheSimGer
{
    public partial class PlayerDongCount : Form
    {
        public static MediaPlayer.MediaPlayer mr = new MediaPlayer.MediaPlayer();
        public static ThePlayerState TheState = null;

        #region 老板键
        private void Form_Activated()
        {
            //注册热键Shift+S，Id号为100。HotKey.KeyModifiers.Shift也可以直接使用数字4来表示。
            HotKey.RegisterHotKey(Handle, 100, HotKey.KeyModifiers.Shift, Keys.S);
            //注册热键Ctrl+B，Id号为101。HotKey.KeyModifiers.Ctrl也可以直接使用数字2来表示。
            HotKey.RegisterHotKey(Handle, 101, HotKey.KeyModifiers.Ctrl, Keys.B);
            //注册热键Alt+D，Id号为102。HotKey.KeyModifiers.Alt也可以直接使用数字1来表示。
            HotKey.RegisterHotKey(Handle, 102, HotKey.KeyModifiers.Alt, Keys.D);
        }
        //在FormA的Leave事件中注销热键。
        private void FrmSale_Leave()
        {
            //注销Id号为100的热键设定
            HotKey.UnregisterHotKey(Handle, 100);
            //注销Id号为101的热键设定
            HotKey.UnregisterHotKey(Handle, 101);
            //注销Id号为102的热键设定
            HotKey.UnregisterHotKey(Handle, 102);
        }

        static bool IsActive = true;
        protected override void WndProc(ref Message m)
        {
            const int WM_HOTKEY = 0x0312;
            //MessageBox.Show("OK!");
            //按快捷键 
            switch (m.Msg)
            {
                case WM_HOTKEY:
                    //MessageBox.Show("OK!");
                    switch (m.WParam.ToInt32())
                    {
                        case 100:  //按下的是Shift+S
                                   //此处填写快捷键响应代码 
                            break;
                        case 101:  //按下的是Ctrl+B
                            Task.Run(() =>
                            {
                                if (IsActive)
                                {
                                    IsActive = false;
                                    Hide();
                                    return;
                                }
                                else
                                {
                                    IsActive = true;
                                    Show();
                                    return;
                                }
                            });
                            break;
                        case 102:  //按下的是Alt+D
                                   //此处填写快捷键响应代码
                            break;
                    }
                    break;
            }
            base.WndProc(ref m);
        }
        #endregion

        #region 日志栏信息更新
        public void AppenGuiLog(string Data)
        {
            BeginInvoke(new Action(() =>
            {
                TheLog.Text += $"{DateTime.Now.ToLocalTime()}=>{Data} {Environment.NewLine}";
                if (TheLog.Text.Length >= 20000) TheLog.Text = "";
            }));
        }
        #endregion

        public bool CheckXingDongCount(int MokudekiCount,int Delay=1000) 
        {
            if (TheState.PlayerDongCount < MokudekiCount) 
            {
                TheWindowsManager.ShowMsg($"您还需要 { (MokudekiCount- TheState.PlayerDongCount) } 行动点!", Delay);
                return false;
            }
            return true;
        }
        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            FrmSale_Leave();
            TheState.SaveToFile();
            //上传用户数据
            //if (IsPlayerCfgReadOk)
            //{
            //    State.Text = "正在同步.....";
            //    var RR = DoUpLoadUserData(this);
            //    State.Text = $"同步{ (RR?"成功":"失败") }.....";
            //}
        }

        public bool IsPlayerCfgReadOk = false;
        /// <summary>
        /// 空间闪动线程
        /// </summary>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Form_Activated(); //启动热键
            TheState = PlayerData.ReadFrmFile();
            if (TheState == null) 
            {
                MessageBox.Show("玩家存档读取失败!");
            }
            //Task.Run(() => 
            //{
            //    Thread.Sleep(3000);
            //    BeginInvoke(new Action(() => new HelpFrm("关于游戏", "第一次进入游戏\r\n帮助信息.......................................................") { StartPosition=FormStartPosition.CenterParent }.ShowDialog()));
            //});

            StartPosition = FormStartPosition.CenterScreen;

            //启动定时数据自动上传
            //ThreadPool.QueueUserWorkItem(TimerUpload);

            //打开配置界面
            Cfg.Click += (sender, @object) => TheWindowsManager.ShowCfgFrm();
            //RemoveSave.Click += (sender, @object) => 
            //{
            //    (sender as PictureBox).Enabled = false;
            //    Task.Run(() =>
            //    {
            //        try
            //        {
            //            var LastColor = (sender as PictureBox).BackColor;
            //            (sender as PictureBox).BackColor = Color.Red;
            //            TheState = new ThePlayerState();
            //            State.Text = "正在同步.....";
            //            var RR = DoUpLoadUserData(this);
            //            State.Text = $"同步{ (RR ? "成功" : "失败") }.....";
            //            (sender as PictureBox).BackColor = LastColor;
            //            if (!RR) Invoke(new Action(() => MessageBox.Show("存档重置失败!")));
            //            Invoke(new Action(() => (sender as PictureBox).Enabled = true));
            //        }
            //        catch (Exception ee) 
            //        {
            //            MessageBox.Show(ee.ToString());
            //        }
            //    });
            //};

            #region 上传用户存档

            //Serializer._OnError = Ex => Invoke(new Action(() => MessageBox.Show(Ex.ToString())));
            //DoUpLoad.Click += (sender, @object) => 
            //{
            //    (sender as PictureBox).Enabled = false;
            //    Task.Run(() => 
            //    {
            //        try
            //        {
            //            var LastColor = (sender as PictureBox).BackColor;
            //            (sender as PictureBox).BackColor = Color.Red;
            //            var RR = DoUpLoadUserData(this);
            //            Invoke(new Action(() => (sender as PictureBox).Enabled = true));
            //            (sender as PictureBox).BackColor = LastColor;
            //            if(!RR) Invoke(new Action(() =>MessageBox.Show("存档上传失败!")));
            //        }
            //        catch (Exception ee) 
            //        {
            //            MessageBox.Show(ee.ToString());
            //        }
            //    });
            //};
            #endregion

            //异步开始获取数据，启动刷新线程
            AutoResetEvent ARE = new AutoResetEvent(false);
            Task.Run(() =>
            {
                //try
                //{
                //    Stopwatch SW = Stopwatch.StartNew();
                //    State.Text = $"获取服务器玩家数据......";
                //    DoReadUserDataAsync().ContinueWith(XX=> 
                //    {
                //        try
                //        {
                //            IsPlayerCfgReadOk = true;
                //            State.Text = $"解析数据完毕......";
                //            SW.Stop();
                //            State.Text = $"获取玩家数据成功 耗时 { SW.ElapsedMilliseconds }";

                //            if (TheState == null)
                //            {
                //                //玩家数据获取失败的情况
                //                //Invoke(new Action(() =>MessageBox.Show("获取玩家数据失败")));
                //                IsPlayerCfgReadOk = false;
                //                TheState = new ThePlayerState();

                //                var UserData1 = Serializer.Serialize(TheState);
                //                var ThePlayerDataJson = "{\"ClientID\":\"123\",\"PlayerData\":\"" + UserData1 + "\"}";
                //                //MessageBox.Show(UserData1);
                //                if (File.Exists(Environment.CurrentDirectory + "\\Debug.txt")) File.Delete(Environment.CurrentDirectory + "\\Debug.txt");
                //                File.WriteAllText(Environment.CurrentDirectory + "\\Debug.txt", ThePlayerDataJson);
                //                //Environment.Exit(0);
                //            }
                //        }
                //        catch (Exception ee)
                //        {
                //            Program.writeLog(ee.ToString());
                //        }
                //        finally 
                //        {
                //            ARE.Set();
                //        }
                //    });
                //}
                //catch (Exception ee)
                //{   
                //    MessageBox.Show(ee.ToString());
                //    State.Text = $"读取数据失败......";
                //}

                try
                {
                    TheUpdate._OnUpdate = () =>
                    {
                        //主界面上控件的刷新方法
                        foreach (var item in Controls)
                        {
                            try
                            {
                                if (item is ShowTheMoney STM) STM.OnUpdate();
                            }
                            catch { }
                            try
                            {
                                if (item is TheSimGer.S.ThePlayerState TPS) TPS.PlayerStateUpdate();
                            }
                            catch { }
                            //刷新玩家BUFF页面
                            try
                            {
                                if (item is PlayerBuffsTab PBT) PBT.DoUpdate();
                            }
                            catch { }
                        }
                    };

                    button3.Enabled = false;
                    //ARE.WaitOne(); //等待网络存档初始化好，在屏蔽网络初始化功能后，这句一定要取消
                    button3.Enabled = true;

                    if (TheState == null)
                    {
                        IsPlayerCfgReadOk = false;
                        TheState = new ThePlayerState();
                        TheState.InitPlayerData();
                    }
                    TheUpdate.Start();
                }
                catch (Exception ee)
                {
                    Program.writeLog(ee.ToString());
                }
                finally
                {
                    AppenGuiLog("游戏刷新系统初始化完毕...");
                }
            });

            //后台音乐线程
            new Thread(() =>
            {
                AppenGuiLog("音乐组件初始化完毕...");
                while (true)
                {
                    try
                    {
                        //label2.Text = $"运行次数{ ++TheIndex }";
                        if (mr.PlayState != MediaPlayer.MPPlayStateConstants.mpPlaying && mr.PlayState != MediaPlayer.MPPlayStateConstants.mpPaused)
                        //if (mr.PlayState != MediaPlayer.MPPlayStateConstants.mpPlaying)
                        {
                            var Files = Directory.GetFiles(Environment.CurrentDirectory + "\\WavBackGroud", "*.mp3");
                            mr.FileName = (Environment.CurrentDirectory + "\\WavBackGroud\\" + Path.GetFileName(Files[TheRandom.Range(0, (Files.Length - 1))]));
                            mr.Play();
                        }
                    }
                    catch { }

                    Thread.Sleep(100);
                }
            })
            { IsBackground = true, }.Start();
        }

        public PlayerDongCount()
        {
            InitializeComponent();
            //TheWindowsManager.OpenPlayerStateFrm();
            //TheState.DebugAddBuff();            
        }

        /// <summary>
        /// 学习
        /// </summary>
        private void button2_Click(object sender, EventArgs e)
        {
            //检查行动点
            if (!CheckXingDongCount(1)) return;
            TheWindowsManager.StartWorkFrm(10, (aa) =>
            {
                //BeginInvoke(new Action(() =>
                //{
                //    MessageBox.Show("任务执行完毕");
                //}));

                var RR = LearnResultManager.GetRandomLearnResult();

                TheWindowsManager.ShowMsg(RR.Item2, 1000);
                TheState.PlayerDongCount--;
                TheState.DoUpdate(RR.Item1);
                return true;
            });
        }


        public void DoSleep()
        {
            //刷新玩家状态每天
            TheState.UpdatePlayerStateGoNext();
            TheState.UpdateBuffsDay();
            var DayPerMoneyCost = TheRandom.Range(1, TheState.PlayerLiveDay * 10);
            TheWindowsManager.ShowMsg($"第 { ++TheState.PlayerLiveDay } 天已经到来！ 账单 { DayPerMoneyCost }", 1000);
            TheState.AddState("CurrentMoney", (DayPerMoneyCost * -1));
            //每天休息的时候休息值随机提高
            //TheState.AddState("XiuXi", TheRandom.Range(1, 50));
            TheState.AddState("ShiWu", -1 * TheRandom.Range(1, 50));
            TheState.AddState("YuLe", -1 * TheRandom.Range(1, 50));
            TheState.IsGambing = false;
        }
        /// <summary>
        /// 进入下一天
        /// </summary>
        private void button3_Click(object sender, EventArgs e)
        {
            //行动点数高于一定值，不允许跳过天数
            if (TheState.PlayerDongCount >= 2)
            {
                TheWindowsManager.ShowMsg($"您今天的行动力还没有用完!", 1000);
                return;
            }

            if (TheWindowsManager.SF._SS == null)
            {
                //最低级睡眠，直接跳过天数，没有任何属性加成
                TheWindowsManager.SF._SS = new Action(() =>
                {
                    DoSleep();
                });
            }
            if (TheWindowsManager.SF._MS == null)
            {
                //最低级睡眠，直接跳过天数，没有任何属性加成
                TheWindowsManager.SF._MS = new Action(() =>
                {
                    TheWindowsManager.StartWorkFrm(10, (aa) =>
                    {
                        //降低食物
                        TheState.AddState("ShiWu", -1 * TheRandom.Range(1, 50));
                        //略微提高休息
                        TheState.AddState("XiuXi", TheRandom.Range(1, 20));
                        var DayPerMoneyCost = TheRandom.Range(1, TheState.PlayerLiveDay * 10);
                        TheState.AddState("CurrentMoney", (DayPerMoneyCost * -4));

                        DoSleep();
                        return true;
                    }, null, "睡眠");

                });
            }
            if (TheWindowsManager.SF._LS == null)
            {
                //最低级睡眠，直接跳过天数，没有任何属性加成
                TheWindowsManager.SF._LS = new Action(() =>
                {
                    var MAXMCount = 1; //最低做梦次数
                    TheWindowsManager.StartWorkFrm(20, (aa) =>
                    {
                        //降低食物
                        TheState.AddState("ShiWu", -1 * TheRandom.Range(50, 150));
                        //略微提高休息
                        TheState.AddState("XiuXi", TheRandom.Range(1, 600));
                        //娱乐
                        TheState.AddState("YuLe", TheRandom.Range(1, 600));
                        var DayPerMoneyCost = TheRandom.Range(1, TheState.PlayerLiveDay * 10);
                        TheState.AddState("CurrentMoney", (DayPerMoneyCost * -10));

                        DoSleep();
                        return true;
                    }, null, "睡眠", (BB) =>
                    {
                        if (TheRandom.Range(1, 1000) < 5 && MAXMCount-- > 0)
                        {
                            TheWindowsManager.ShowMsg($"好梦:自己正抱着宠物睡觉，娱乐++！", 1000);
                            TheState.AddState("YuLe", TheRandom.Range(1, 200));
                        }
                        return (true, "");
                    });

                });
            }
            TheWindowsManager.SF.ShowDialog();
        }

        /// <summary>
        /// 户外寻宝
        /// </summary>
        private void button4_Click(object sender, EventArgs e)
        {
            if (!CheckXingDongCount(1)) return;
            TheState.PlayerDongCount--;
            TheWindowsManager.ShowItemFindFrm();
        }

        /// <summary>
        /// 查看BUFF
        /// </summary>
        private void button5_Click(object sender, EventArgs e)
        {
            TheWindowsManager.ShowBuffFrm();
        }

        /// <summary>
        /// 散心
        /// </summary>
        private void button6_Click(object sender, EventArgs e)
        {
            if (TheState.PlayerDongCount == 0)
            {
                TheWindowsManager.ShowMsg($"您的行动力已经用完!", 1000);
                return;
            }

            TheWindowsManager.StartWorkFrm(10, (aa) =>
            {
                var XiuXiAdd = TheRandom.Range(1, 25);
                TheWindowsManager.ShowMsg($"事件=>休息完毕，休息值+{ XiuXiAdd }", 1000);
                TheState.AddState("XiuXi", XiuXiAdd);
                TheState.PlayerDongCount--; //行动力更新

                if (TheRandom.Range(1, 1000) <= 533 && TheState._PlayerBuffs.Count(XX => XX.Name == "心情略微良好") == 0)
                {
                    TheWindowsManager.ShowMsg($"事件=>眺望远处发呆时获得了一个小灵感 获得新BUFF:心情略微良好 \r\n 一段时间内微小幅度提高休息值", 1000);
                    TheState._PlayerBuffs.Add(new TheBuff
                    {
                        Name = "心情略微良好",
                        Comment = "一段时间内微小幅度提高休息值",
                        TTL = TheRandom.Range(100, 300),
                        _OnTick = new Action(() =>
                        {
                            if (TheRandom.Range(1, 1000) <= 400)
                            {
                                var Add = 1;
                                TheState.AddState("XiuXi", Add);
                            }
                        }),
                        BZF = BuffZhengFu.Positive,
                    });
                    //TheState.AddState("NengLi", NengLiAdd);
                }

                return true;
            });
        }

        /// <summary>
        /// 进行网络游戏
        /// </summary>
        private void button7_Click(object sender, EventArgs e)
        {
            if (TheState._PlayerBuffs.Count(XX => XX.Name == "网络沉醉") != 0)
            {
                TheWindowsManager.ShowMsg($"事件=>您必须等待【网络沉醉】BUFF消息后，再进行网络游戏行动", 1000);
                return;
            }

            if (TheState.PlayerDongCount == 0)
            {
                TheWindowsManager.ShowMsg($"您的行动力已经用完!", 1000);
                return;
            }

            TheWindowsManager.StartWorkFrm(10, (aa) =>
            {
                var XiuXiAdd = TheRandom.Range(1, 500);
                TheWindowsManager.ShowMsg($"事件=>疯狂沉迷网络世界+{ XiuXiAdd }", 1000);
                TheState.AddState("YuLe", XiuXiAdd);
                TheState.PlayerDongCount--; //行动力更新

                TheWindowsManager.ShowMsg($"事件=>网络沉醉，在一段时间内不能再沉迷网络游戏，否则将会危及性命", 1000);
                TheState._PlayerBuffs.Add(new TheBuff
                {
                    Name = "网络沉醉",
                    Comment = "网络耐性大幅度下降，在一段时间内无法再进行网络游戏",
                    TTL = TheRandom.Range(100, 300),

                    BT = BuffType.Day,
                    TTLDay = TheRandom.Range(1, 3),
                    BZF = BuffZhengFu.Negative,
                });
                //TheState.AddState("NengLi", NengLiAdd);

                return true;
            });
        }

        /// <summary>
        /// 兼职
        /// </summary>
        private void button8_Click(object sender, EventArgs e)
        {
            if (TheState.PlayerDongCount < 2)
            {
                TheWindowsManager.ShowMsg($"兼职需要2点行动点数，请保证角色进行良好的休息后，再进行兼职!", 1000);
                return;
            }

            if (TheState.NengLi <= 500 || TheState.YuLe <= 300)
            {
                TheWindowsManager.ShowMsg($"您目前状态不太好，建议进行良好休息后再进行兼职", 1000);
                return;
            }

            if (TheState.CurrentMoney >= 10000)
            {
                TheWindowsManager.ShowMsg($"花一些钱再来兼职！", 1000);
                return;
            }

            TheState.PlayerDongCount -= 2;
            TheWindowsManager.StartWorkFrm(50, (aa) =>
            {
                //计算兼职薪酬
                var MoneyAdd = TheRandom.Range(20, 100) + (TheState.JianZhiNengLi * TheRandom.Range(1, 4));

                //玩家简直能力提升
                TheState.JianZhiNengLi++;

                var NX = (NengLi: (TheRandom.Range(1, 200) * -1), XiuXi: (TheRandom.Range(1, 100) * -1));
                TheState.AddState("NengLi", NX.NengLi);
                TheState.AddState("XiuXi", NX.XiuXi);
                TheState.AddState("CurrentMoney", MoneyAdd);

                TheWindowsManager.ShowMsg($"兼职工资=>工资到账+{ MoneyAdd  } 能力{ NX.NengLi } 休息{ NX.XiuXi }", 1000);

                if (TheRandom.Range(1, 1000) <= 133 && TheState._PlayerBuffs.Count(XX => XX.Name == "郁闷") == 0)
                {
                    Invoke(new Action(() => TheWindowsManager.ShowMsg($"事件=>兼职的时候和同事发生了一些不愉快的事情", 1000)));
                    TheState._PlayerBuffs.Add(new TheBuff
                    {
                        Name = "郁闷",
                        Comment = "一段时间内微小幅度提高休息值",
                        TTL = TheRandom.Range(30, 100),
                        _OnTick = new Action(() =>
                        {
                            if (TheRandom.Range(1, 1000) <= 400)
                            {
                                var Add = -1;
                                TheState.AddState("YuLe", Add);
                            }
                        }),
                        BZF = BuffZhengFu.Negative,
                    });
                    //TheState.AddState("NengLi", NengLiAdd);
                    return true;
                }

                if (TheRandom.Range(1, 1000) <= 133 && TheState._PlayerBuffs.Count(XX => XX.Name == "劳累") == 0)
                {
                    Invoke(new Action(() => TheWindowsManager.ShowMsg($"事件=>高强度兼职后，变的非常劳累", 1000)));
                    TheState._PlayerBuffs.Add(new TheBuff
                    {
                        Name = "劳累",
                        Comment = "一段时间内微小幅度提高休息值",
                        TTL = TheRandom.Range(30, 133),
                        _OnTick = new Action(() =>
                        {
                            if (TheRandom.Range(1, 1000) <= 400)
                            {
                                var Add = -1;
                                TheState.AddState("XiuXi", Add);
                            }
                        }),
                        BZF = BuffZhengFu.Negative,
                    });
                    //TheState.AddState("NengLi", NengLiAdd);
                    return true;
                }
                //TheState.AddState("NengLi", NengLiAdd);

                return true;
            });
        }

        /// <summary>
        /// 用餐
        /// </summary>
        private void button9_Click(object sender, EventArgs e)
        {
            if (TheState.PlayerDongCount <= 0)
            {
                TheWindowsManager.ShowMsg($"用餐需要1行动点数!", 1000);
                return;
            }

            if (TheState.IsEat)
            {
                TheWindowsManager.ShowMsg($"你已经去过餐厅了!", 1000);
                return;
            }

            TheState.PlayerDongCount--;
            TheWindowsManager.ShowFoodSellFrm();
            TheState.IsEat = true;

            //TheWindowsManager.StartWorkFrm(40, (aa) =>
            //{
            //    var FoodAdd = TheRandom.Range(20, 60);
            //    TheState.AddState("ShiWu", FoodAdd);
            //    TheWindowsManager.ShowMsg($"吃饭=>食物{ FoodAdd }", 1000);

            //    if (TheRandom.Range(1, 1000) <= 133 && TheState._PlayerBuffs.Count(XX => XX.Name == "胃胀气") == 0)
            //    {
            //        TheState.PlayerDongCount--;
            //        FoodAdd = TheRandom.Range(20, 60);
            //        TheState.AddState("ShiWu", FoodAdd);
            //        TheWindowsManager.ShowMsg($"吃饭=>吃的太多，胃胀气 食物额外{ FoodAdd }", 1000);
            //        TheState._PlayerBuffs.Add(new TheBuff
            //        {
            //            Name = "胃胀气",
            //            Comment = "小段时间内，食物值缓慢下降",
            //            TTL = TheRandom.Range(30, 133),
            //            _OnTick = new Action(() =>
            //            {
            //                if (TheRandom.Range(1, 1000) <= 200)
            //                {
            //                    var Add = -1;
            //                    TheState.AddState("ShiWu", Add);
            //                }
            //            }),
            //            BZF = BuffZhengFu.Negative,
            //        });
            //        //TheState.AddState("NengLi", NengLiAdd);
            //        return true;
            //    }

            //    return true;
            //},null,"吃饭");
        }

        /// <summary>
        /// 打开邮件
        /// </summary>
        private void button10_Click(object sender, EventArgs e)
        {
            TheWindowsManager.ShowMailList();
        }

        /// <summary>
        /// 能力培训
        /// </summary>
        private void button11_Click(object sender, EventArgs e)
        {
            if (TheState.PlayerDongCount <= 0)
            {
                TheWindowsManager.ShowMsg($"能力培训需要1行动点数!", 1000);
                return;
            }

            if (TheState.XiuXi <= 200)
            {
                TheWindowsManager.ShowMsg($"您必须进行足够的休息后方可进行能力培训!", 1000);
                return;
            }

            //随机学习时间
            var LearnTime = 120;
            //30%概率缩短学习时间
            if (TheRandom.Range(0, 1000) < 300) LearnTime = 85;
            if (TheRandom.Range(0, 1000) < 55) LearnTime = 55;

            TheWindowsManager.StartWorkFrm(LearnTime, (aa) =>
            {
                TheState.PlayerDongCount--;
                TheState.JianZhiLearnCount++; //玩家兼职次数加1
                TheState.JianZhiNengLi += TheRandom.Range(1, 3);
                //TheWindowsManager.ShowMsg($"兼职能力提升!", 1000);
                Invoke(new Action(() => TheWindowsManager.ShowMsg($"兼职能力提升!", 1000)));
                //随机扣除休息值
                TheState.AddState("XiuXi", TheRandom.Range(1, 100) * -1);
                return true;
            });
        }

        /// <summary>
        /// 宠物店
        /// </summary>
        private void button12_Click(object sender, EventArgs e)
        {
            if (TheState.PlayerDongCount <= 0)
            {
                TheWindowsManager.ShowMsg($"进入宠物店需要1行动点数!", 1000);
                return;
            }

            //进入宠物店消耗一点体力
            TheState.PlayerDongCount--;
            //打开宠物店窗口
            TheWindowsManager.ShowPetGet();
        }

        /// <summary>
        /// 联系人
        /// </summary>
        private void button14_Click(object sender, EventArgs e)
        {
            TheWindowsManager.ShowManList();
        }

        /// <summary>
        /// 溜进邻居家
        /// </summary>
        private void button13_Click(object sender, EventArgs e)
        {
            if (!TheState.CheckDongCount()) return;  //检查玩家行动点数
            if (TheState._PlayerBuffs.Count(XX => XX.Name == "被监视") != 0)
            {
                TheWindowsManager.ShowMsg($"您已经被监视，无法再进行偷窃行动", 1000);
                return;
            }

            TheState.PlayerDongCount--;

            int GoldFindCountLimite = 0;
            (sender as Button).Enabled = false;
            //一定时间内不允许偷窃行为
            new Thread(() => { button13.Text = "安保系统运作中..."; Thread.Sleep(1000 * TheRandom.Range(100, 300)); button13.Enabled = true; button13.Text = "溜进邻居家"; }) { IsBackground = true, }.Start();
            TheWindowsManager.StartWorkFrm(200, (aa) =>
            {

                TheState.StoneCount++;

                TheWindowsManager.ShowMsg($"偷窃到了大量金钱", 1000);
                //计算偷窃到的金钱数量
                TheState.CurrentMoney += (TheRandom.Range(1, 500) + TheState.StoneCount * TheRandom.Range(1, 50));
                return true;
            }, (aa) =>
            {
                //TheState.PlayerDongCount++;
                TheWindowsManager.ShowMsg($"你仓皇逃窜........", 1000);
                return true;
            }, "偷窃中", (bb) =>
            {
                //是否发现额外的宝物

                if (TheRandom.Range(1, 1000) < 11 && ++GoldFindCountLimite < 5)
                {
                    Invoke(new Action(() =>
                    {
                        TheWindowsManager.ShowMsg($"你在邻居家发现了一些值钱的东西", 200);
                        TheState.CurrentMoney += (TheRandom.Range(1, 100) + TheState.StoneCount * TheRandom.Range(1, 5));
                    }));
                }

                if (TheRandom.Range(1, 1000) < 3)
                {
                    Invoke(new Action(() =>
                    {
                        TheWindowsManager.ShowMsg($"你在偷窃的过程中被邻居发现......", 200);
                        Invoke(new Action(() => TheWindowsManager.ShowMsg($"事件=>高强度兼职后，变的非常劳累", 1000)));

                        //加上被监视BUFF
                        TheState._PlayerBuffs.Add(new TheBuff
                        {
                            Name = "被监视",
                            Comment = "在一定时间内无法再进行偷窃行动，由于偷窃行为，您在所有联系人中的好感度会缓缓下降",
                            TTL = TheRandom.Range(230, 633),
                            BZF = BuffZhengFu.Negative,
                        });

                        //TheState.CurrentMoney += (TheRandom.Range(1, 100) + TheState.StoneCount * TheRandom.Range(1, 5));                        
                    }));
                    return (false, "");
                }

                return (true, "");
            });
        }

        /// <summary>
        /// 赌博
        /// </summary>
        private void button15_Click(object sender, EventArgs e)
        {
            if (TheState.PlayerDongCount <= 0)
            {
                TheWindowsManager.ShowMsg($"赌博需要1行动点数!", 1000);
                return;
            }

            if (TheState.IsGambing)
            {
                TheWindowsManager.ShowMsg($"您今天已经去过赌场了!", 1000);
                return;
            }

            //每天只能进一次赌场
            TheState.IsGambing = true;

            //进入宠物店消耗一点体力
            TheState.PlayerDongCount--;
            TheWindowsManager.ShowGamblingHouse();
        }

        private void button16_Click(object sender, EventArgs e)
        {
            FrmTest FT = new FrmTest();
            FT.Show();
        }

        /// <summary>
        /// 喝酒
        /// </summary>
        private void button18_Click(object sender, EventArgs e)
        {
            if (TheState._PlayerBuffs.Count(XX => XX.Name == "醉酒(暖身)") != 0)
            {
                TheWindowsManager.ShowMsg($"你喝的太多了", 1000);
                return;
            }

            var ThePriceFoodForPet = TheRandom.Range(100, 250);
            var RR = PlayerDongCount.TheState.TryJianMoney(ThePriceFoodForPet);
            if (!RR)
            {
                TheWindowsManager.ShowMsg($"事件=>您没有足够的金钱为她购买礼物", 1000);
                return;
            }

            //开始喝酒
            TheWindowsManager.StartWorkFrm(TheRandom.Range(10, 20), (aa) =>
            {
                //添加醉酒BUFF
                TheState._PlayerBuffs.Add(new TheBuff
                {
                    Name = "醉酒(暖身)",
                    Comment = "酒精的效果，每天获得1额外心动点数",
                    TTL = TheRandom.Range(100, 300),

                    BT = BuffType.Day,
                    TTLDay = TheRandom.Range(1, 6),
                    BZF = BuffZhengFu.Positive,
                    _OnDayPassed = () =>
                    {
                        TheState.PlayerDongCount++; //获得额外的行动点数
                    }
                });
                return true;
            }, null, "喝酒中", (bb) =>
            {
                //喝酒过程中随机触发事件

                return (true, "");
            });
        }

        /// <summary>
        /// 根据给出的地图，让玩家进行小范围探索
        /// </summary>
        private void button1_Click(object sender, EventArgs e)
        {
            Map _Map = new Map();
            _Map.Name = "附近荒野";
            _Map.TimeTake = TheRandom.Range(10, 20);
            _Map._BeginSerch = null;
            _Map._EndSerch = new Action(() => 
            {
                TheWindowsManager.ShowMsg($"事件=>探索完毕 ", 100);
            });

            if (TheState._TheMaps.Count == 0)
            {
                TheState._TheMaps.Add(_Map);

                var _Map1 = new Map();
                _Map1.Name = "医院";
                _Map1.TimeTake = TheRandom.Range(40, 100);
                _Map1._BeginSerch = null;
                _Map1._EndSerch = new Action(()=> 
                {
                    TheWindowsManager.ShowMsg($"医院：移除随机负面BUFF", 1000);
                    var TriggerBuff = PlayerDongCount.TheState._PlayerBuffs.Where(XX => XX.BZF == BuffZhengFu.Negative).ToList().GetRandom();
                    PlayerDongCount.TheState._PlayerBuffs.Remove(TriggerBuff);
                    TheWindowsManager.TML?.Close();

                    if (TheState.TotalHeal <= 200) TheState.TotalHeal += 100; //随机恢复一定健康
                    return;
                });

                _Map1._OnMapRoadTick = new Action<object>((aa) => 
                {
                    if (aa is TheMap TheMap) 
                    {
                        //去医院的路上随机消耗食物
                        if(TheRandom.Range(1,1000)<200) TheState.AddState("ShiWu", -1);
                    }
                });

                TheState._TheMaps.Add(_Map1);
            }

            TheWindowsManager.TML = new PlayerState.TheMapList();
            //释放窗口资源
            TheWindowsManager.TML.FormClosing += (aa, bb) => { TheWindowsManager.TML.Dispose(); TheWindowsManager.TML = null; };
            TheWindowsManager.TML.ShowDialog();
        }

        /// <summary>
        /// 上班
        /// </summary>
        private void button5_Click_1(object sender, EventArgs e)
        {
            if (!CheckXingDongCount(3)) return;

            if (TheState.NengLi <= 500 || TheState.YuLe <= 300)
            {
                TheWindowsManager.ShowMsg($"您目前状态不太好，建议进行良好休息后再进行兼职", 1000);
                return;
            }

            if (TheState.CurrentMoney >= 100000)
            {
                TheWindowsManager.ShowMsg($"花一些钱再来上班！", 1000);
                return;
            }

            //上班减娱乐
            TheState.AddState("YuLe", -1 * TheRandom.Range(1, 200));
            TheState.PlayerDongCount -= 3;

            //开始喝酒
            TheWindowsManager.StartWorkFrm(TheRandom.Range(30, 200), (aa) =>
            {
                //计算兼职薪酬
                var MoneyAdd = TheRandom.Range(200, 1000) + (TheState.JianZhiNengLi * TheRandom.Range(1, 10));

                TheState.AddState("CurrentMoney", MoneyAdd);

                TheWindowsManager.ShowMsg($"上班工资=>上班工资到账+{ MoneyAdd  } ", 1000);
                return true;
            }, null, "工作中", (bb) =>
            {
                //喝酒过程中随机触发事件

                return (true, "");
            });
        }

        /// <summary>
        /// 做饭
        /// </summary>
        private void button17_Click(object sender, EventArgs e)
        {
            if (!CheckXingDongCount(2)) return;

            //做饭随机增加娱乐
            TheState.AddState("YuLe", 1 * TheRandom.Range(50, 100));
            TheState.PlayerDongCount -= 2;

            TheWindowsManager.StartWorkFrm(TheRandom.Range(20, 60), (aa) =>
            {
                if (TheRandom.Range(1, 1000) < 100)
                {
                    TheWindowsManager.ShowMsg($"上班工资=>厨艺进步!", 1000);
                    TheState.PlayerChuYi++;
                }
                return true;
            }, null, "做饭", (bb) =>
            {
                //喝酒过程中随机触发事件
                if (TheRandom.Range(1, 1000) < 400) TheState.AddState("ShiWu", (1+ TheState.PlayerChuYi));
                if (TheRandom.Range(1, 1000) < 400) TheState.CurrentMoney += (1 + TheState.PlayerChuYi);

                return (true, "");
            });
        }
    }
}
